//
//  SongListsViewCtl.m
//  WYMusic
//
//  Created by wy on 16/9/17.
//  Copyright © 2016年 wy. All rights reserved.
//

#import "SongListsViewCtl.h"
#import "PlayViewCtl.h"
#import "ViewController.h"
#import "NetworkRequest.h"
#import "SongsListModel.h"
#import "SongsListCell.h"
#import "UIImageView+WebCache.h"
#import "PersonalSongsViewCtl.h"
#import "AVPlayerManager.h"
#import "MJRefresh.h"

#define SCREEN_WIDTH ( [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ( [[UIScreen mainScreen] bounds].size.height)
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f \
blue:(b)/255.0f alpha:1.0f]


@interface SongListsViewCtl ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *wCollection;
@property(nonatomic,strong)NSMutableArray *data;

@property(nonatomic,assign)NSInteger pageSize;
@end

@implementation SongListsViewCtl


-(NSMutableArray *)data{
    if ( !_data) {
        _data =[NSMutableArray array];
    }
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.navigationItem.title = @"最热歌单";
    self.pageSize = 0;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/2-20, 180);
    layout.minimumInteritemSpacing = 10 ;
    layout.minimumLineSpacing = 15 ;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.wCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    self.wCollection.backgroundColor =RGBCOLOR(22, 27, 33); //22 27 33
    
    self.wCollection.delegate = self;
    self.wCollection.dataSource =self;
    
    [self.wCollection registerNib:[UINib nibWithNibName:@"SongsListCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.wCollection];
    
    [self refeshUpDown];
    
    [self loadData];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 80, 40);
    [button setTitle:@"播放器" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button addTarget:self action:@selector(playView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *playButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = playButton;
    
}

-(void)refeshUpDown{
    __weak typeof(self) weekSelf = self;
    
    NSArray *idleImages = [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"smiley_0"],
                           [UIImage imageNamed:@"smiley_1"],
                           [UIImage imageNamed:@"smiley_2"],
                           [UIImage imageNamed:@"smiley_3"],
                           [UIImage imageNamed:@"smiley_4"],
                           nil];
    NSArray *pullingImages = [NSArray arrayWithObjects:
                              [UIImage imageNamed:@"smiley_0"],
                              nil];
    NSArray *refreshingImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"smiley_0"],
                                 [UIImage imageNamed:@"smiley_1"],
                                 [UIImage imageNamed:@"smiley_2"],
                                 [UIImage imageNamed:@"smiley_3"],
                                 [UIImage imageNamed:@"smiley_4"],
                                 nil];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
         weekSelf.pageSize =0;
        [weekSelf loadData];
    }];
    
    //1.设置普通状态的动画图片
    [header setImages:idleImages forState:MJRefreshStateIdle];
    //2.设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:pullingImages forState:MJRefreshStatePulling];
    //3.设置正在刷新状态的动画图片
    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    
    self.wCollection.mj_header = header;
    
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter  footerWithRefreshingBlock:^{
        [weekSelf loadData];
    }];
    
    //1.设置普通状态的动画图片
    [footer setImages:idleImages forState:MJRefreshStateIdle];
    //2.设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [footer setImages:pullingImages forState:MJRefreshStatePulling];
    //3.设置正在刷新状态的动画图片
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    self.wCollection.mj_footer = footer;
    
}
-(void)loadData{
    self.pageSize += 10;
    NSString *url = @"http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=1&size=";
    NetworkRequest *request =[NetworkRequest shareInstance];
    [request getRequest:[NSString stringWithFormat:@"%@%ld",url,self.pageSize] parameter:nil success:^(id responseObject) {
        
        [self.data removeAllObjects];
        
        if ([responseObject[@"code"]integerValue] ==1) {
            NSArray *dataArray =responseObject[@"data"];
            for (NSDictionary *obj in dataArray) {
                SongsListModel *mod =[[SongsListModel alloc] init];
                [mod setValuesForKeysWithDictionary:obj];
                [self.data addObject:mod];
            }
        }
        [self.wCollection reloadData];
        [self.wCollection.mj_header endRefreshing];
        [self.wCollection.mj_footer endRefreshing];
    } failuer:^(NSError *error) {
        
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SongsListCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    SongsListModel *mod = self.data[indexPath.item];
    [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:mod.pic_url] placeholderImage:nil];
    cell.authorLabel.text = mod.author;
    cell.titleName.text = mod.title;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SongsListModel *mod = self.data[indexPath.item];
    PersonalSongsViewCtl *vc =[[PersonalSongsViewCtl alloc] init];
        vc._id = mod._id;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)playView{
    
    PlayViewCtl *vc = [[PlayViewCtl alloc] init];
    vc.musicManager = [AVPlayerManager shareManager];
    vc.index =  [vc.musicManager getcurrentItem];
    vc.musicManager.listInto = NO;
    
    vc.songArray = vc.musicManager.songArray;
    vc.singerArray = vc.musicManager.singerArray;
    vc.imageArray = vc.musicManager.imageArray;
    vc.songNameArray = vc.musicManager.songNameArray;
    
    [self presentViewController:vc animated:YES completion:nil];
  
}

@end
